import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtCore import Qt #其中有许多常量
 
 
class QComboBoxDemo(QWidget):
    def __init__(self):
        super(QComboBoxDemo, self).__init__()
        self.initUI()
 
 
    def initUI(self):
        self.setWindowTitle('QComboBoxDemo')
        self.resize(350,100)
        #垂直布局
        layout=QVBoxLayout()
 
        self.label=QLabel('请选择编程语言')
        self.cb=QComboBox()
        self.cb.addItem('C++')
        self.cb.addItem('Python')
        self.cb.addItems(['Java','C#','Ruby'])
        #每一项都对应一个下标索引
        self.cb.currentIndexChanged.connect(self.selectionChange)#此信号默认传递两个参数(控件本身,索引)
 
        layout.addWidget(self.label)
        layout.addWidget(self.cb)
 
        self.setLayout(layout)
 
    def selectionChange(self,i):
        #标签会随着当前选中的下拉项而改变
        self.label.setText(self.cb.currentText())
        self.label.adjustSize()
 
        for j in range(self.cb.count()):
            print('item'+str(j)+'='+self.cb.itemText(j))
 
        print('current index',i,'selection changed',self.cb.currentText())
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=QComboBoxDemo()
    main.show()
    sys.exit(app.exec_())
